$(function(){
    /**
     * 分页初始条件
     */
    var total_count = 1;//分页总记录数
    var total = 1;//分页总页面数
    var currentPage = 1;//当前页
    var pageSize = pageOption.pageSize;//每页显示的记录数
    var idList = [];//批量选择id所存的数组
    var root_url = $("#root_url").val();
    var phone_list = $('.phone-list').val();
    var uids = $('.uids').val();

    /**
     * 页面初始化
     */
    function init(){
        myPagination();
        bindEvent();
    }

    /**
     * 事件绑定
     */
    function bindEvent(){
        var index = parent.layer.getFrameIndex(window.name);
        //根据条件查询订单信息
        $('.inner-section #search_btn').click(function() {
            render(true,1,pageSize);
            return false;
        });
        //enter键盘事件
        $(".inner-section .search-param-form input").keydown(function(event){
            event = event ? event: window.event;
            if(event.keyCode == 13){
                render(true,1,pageSize);
                return false;
            }
        });
    }
    /**
     * 获取模糊参数
     */
    function getSelectInfo(){
        var selectInfo = {};
        $.each($('#search-param-form').serializeArray(), function(i, field){
            selectInfo[field.name]=field.value;
        });
        JSON.stringify(selectInfo);
        return selectInfo;
    }
    /**
     * 分页显示方法
     */
    function myPagination(){
        render(true,1,pageSize);
        //调用公共分页方法
        pagination("#page-selection",{pageSize:pageSize,total:total},render);
    }
    /**
     * 分页动态渲染数据
     * @param async ajax请求是否异步
     * @param pageIndex 当前显示页
     * @param pageSize 每页显示记录数
     */
    function render(async,pageIndex,pageSize){
        var selectInfo = getSelectInfo();
        selectInfo.pageIndex = pageIndex;
        selectInfo.pageSize = pageSize;
        $.ajax({
            async:async,
            type:'post',
            url:'pagingUser',
            data:selectInfo,//从1开始计数
            dataType:'json',
            success:function(result){
                var html ='';
                if(result.errorCode == 0){
                    total = result.data.pageInfo.total_page;
                    total_count = result.data.pageInfo.total_count;
                    $("#page-selection").bootpag({total:total,total_count:total_count});//重新计算总页数,总记录数

                    currentPage = result.data.pageInfo.current_page;
                    var userList = result.data.dataList;

                    html+='<tr>' +
                        '<th class="" style="width: 80px;"></th>' +
                        '<th class="">序号</th>' +
                        '<th class="">头像</th>' +
                        '<th class="">昵称</th>' +
                        '<th class="">姓名</th>' +
                        '<th class="">手机号码</th>' +
                        '<th class="">省份</th>' +
                        '<th class="">城市</th>' +
                        '<th class="">身份</th>' +
                        '</tr>';
                    var colspan = $(html).find("th").length;
                    for(var i = 0; i < userList.length;i++){
                        var obj = userList[i];
                        var num = (pageIndex - 1) * pageSize + i + 1;
                        var head_url = obj.head_img_url;
                        var province = obj.province || '--';
                        var city = obj.city || '--';
                        var mobile = obj.mobile || '--';
                        var nick_name = obj.nick_name || '--';
                        var username = obj.username || '--';
                        var identity_text = obj.identity_text;
                        var account = obj.account||'--';
                        var add_time = obj.add_time || '--';
                        var id = obj.id;
                        var store = parseInt(obj.store);
                        var checked = (idList.indexOf(id.toString())>=0) ? "checked":"";//判断当前记录先前有没有被选中
                        var store_html = ''
                        // if (!store) {
                            store_html = '<input type="radio" name="id" class="select-single" data-identity="'+obj.identity+'" data-nickname="'+obj.nick_name+'" data-id="'+id+'" '+checked+'>'
                        // }
                        html+='<tr>'
                            +'<td>'+store_html+'</td>'
                            +'<td>'+num+'</td>'
                            +'<td>'+(head_url?'<img width="50" src="'+head_url+'">':'无头像')+'</td>'
                            +'<td><span class="limit-text" title="'+nick_name+'">'+nick_name+'</span></td>'
                            +'<td>'+username+'</td>'
                            +'<td>'+mobile+'</td>'
                            +'<td>'+province+'</td>'
                            +'<td>'+city+'</td>'
                            +'<td>'+identity_text+'</td>'
                    }
                    if(userList.length == 0){
                        html += '<tr><td colspan="'+colspan+'"><p class="text-danger">暂无数据。</p></td></tr>';
                    }
                    $(".inner-section #list-table tbody").html(html);
                    //选择
                    $('.select-single').click(function () {
                        var item = {
                            id:$(this).attr('data-id'),
                            nick_name:$(this).attr('data-nickname'),
                            identity:$(this).attr('data-identity'),
                        };
                        // if (item.identity.indexOf('3') != '-1') {
                        //     return layer.alert('该用户是红娘身份，不能选择');
                        // }
                        if ($(this).prop('checked')) {
                            idList.push(item)
                        }
                    })
                }else{
                    responseTip(result.errorCode,json.errorInfo,1500);
                }

            },
            error:errorResponse
        });
    }

    getIdList = function(){
        return {
            user: idList[0] || -1,
        }
    }

    init();
});
function getData(){
    return getIdList() ;
}